﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;

using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;

namespace ZF.WS.Admin.Member
{
    public partial class IntegralLog : ZF.BLL.Page.MMPage
    {
        [PageArgument(Key = "Id", DefaultValue = 0)]
        public int MemberId { get; set; }
        public override ModuleType Module
        {
            get { return ModuleType.JFTJ; }
            protected set
            {
                base.Module = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitControls();
                View();
            }
        }
        public void InitControls()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                this.ddlTitle.DataSource = dc.US_IntegralLog.Select(p => p.Title).Distinct().ToList()
                      .Select(p => new
                      {
                          Key = p,
                          Text = p,
                      }).ToList();
                this.ddlTitle.DataBind();
            }
        }
        public void View()
        {
            ListBind();
     
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {


                List<Expression<Func<Model.US_IntegralLog, bool>>> filterList = new List<Expression<Func<Model.US_IntegralLog, bool>>>();
                IQueryable<Model.US_IntegralLog> selector = dc.US_IntegralLog;
                if (MemberId > 0)
                {
                    filterList.Add(p => p.MemberId == MemberId);
                }

                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }

              

                var selWhere = selector.Select(p =>
                    new
                    {
                        Log = p,
                        Member = p.US_Member,
                    }).Where(this.gvList);






                var selPage = selWhere.OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);




                this.gvList.RowCount = selWhere.Count();
                var source = selPage.ToList();
                this.gvList.DataSource = source;
                this.gvList.DataBind();
                TJ();

            }
        }

        public void TJ()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {


                List<Expression<Func<Model.US_IntegralLog, bool>>> filterList = new List<Expression<Func<Model.US_IntegralLog, bool>>>();
                IQueryable<Model.US_IntegralLog> selector = dc.US_IntegralLog;
                if (MemberId > 0)
                {
                    filterList.Add(p => p.MemberId == MemberId);
                }

                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }
                var selWhere = selector.Select(p => new
               {
                   Log = p,
                   Member = p.US_Member,
               }).Where(this.gvList);
                var group = selWhere.Select(p=>p.Log).ToList().GroupBy(p => p.Title).ToList();

                var tjlist = group.Select(p => new TJModel()
                {
                    Title = p.Key,
                    Inte = p.Sum(s => s.Integral),
                });

                this.lvTJ.DataSource = tjlist;
                this.lvTJ.DataBind();

            }
        }


        class TJModel
        {
            public string Title { get; set; }
            public int Inte { get; set; }
        }

        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            //using (var dc = BLL.SiteInfo.CreateDBDataContext())
            //{
            //    bool isSuccess = true;
            //    try
            //    {
            //        dc.US_Member.DeleteAllOnSubmit(
            //           dc.US_Member.Where(p => idArr.Contains(p.MemberId)));
            //        dc.SubmitChanges();
            //    }
            //    catch (Exception ex)
            //    {
            //        isSuccess = false;
            //        throw SiteInfo.WarnFactory.CreateWarn(ex);
            //    }
            //    string msgKey = MemberType == 100 ? "系统管理员" : "会员";
            //    if (isSuccess)
            //        WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            //}
        }
        public void Delete(object sender, EventArgs e)
        {
            //var bc = (IButtonControl)sender;
            //if (bc.CommandArgument == "select")
            //{
            //    var idArrTemp = this.gvList.GetCheckKeys();
            //    if (idArrTemp.Length == 0)
            //        throw SiteInfo.WarnFactory.CreateWarn(using (var dc = BLL.SiteInfo.CreateDBDataContext())
            //{
             
                 
            //    List<Expression<Func<Model.US_IntegralLog, bool>>> filterList = new List<Expression<Func<Model.US_IntegralLog, bool>>>();
            //    IQueryable<Model.US_IntegralLog> selector = dc.US_IntegralLog;
            //    if (MemberId > 0)
            //    { 
            //        filterList.Add(p => p.MemberId == MemberId);
            //    }
                 
            //    foreach (var filter in filterList)
            //    {
            //        selector = selector.Where(filter);
            //    }

            //    selector.ToList().GroupBy(p => p.Title).ToList();

            //    var selWhere = selector.Select(p =>
            //        new
            //        {
            //            Log=p,
            //            Member = p.US_Member,
            //        }).Where(this.gvList);

                 




            //    var selPage = selWhere.OrderBy(this.gvList.SortExpression)
            //            .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);




            //    this.gvList.RowCount = selWhere.Count();
            //    var source = selPage.ToList();
            //    this.gvList.DataSource = source;
            //    this.gvList.DataBind();

            //}"未选择任何记录");
            //    var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
            //    Delete(idArr);
            //}
            //else
            //{
            //    Delete(bc.CommandArgument.Convert<int>());
            //}
        }
        public void Search(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            if (this.tbAccount.Text.EmptyString() != null)
            {
                if (this.rblCT.SelectedValue == "0")
                    this.gvList.AddFilterExpression("Member.Account.Contains(@0)", this.tbAccount.Text);
                else
                    this.gvList.AddFilterExpression("Member.Account=@0", this.tbAccount.Text);
                this.MemberId = 0;
            }
            if (this.ddlTitle.SelectedValue != "")
                this.gvList.AddFilterExpression("Log.Title=@0", this.ddlTitle.Text);
            if (this.tbType.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Log.Title=@0", this.tbType.Text);
            if (this.tbBeginTime.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Log.AddTime>=@0", this.tbBeginTime.Text.Convert<DateTime>());
            if (this.tbEndTime.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Log.AddTime<=@0", this.tbEndTime.Text.Convert<DateTime>());
            this.gvList.PageIndex = 0;
            ListBind();
        }

        public void TJ(object sender, EventArgs e)
        {
            if (this.tbAccount.Text.EmptyString() != null)
            {
                if (this.rblCT.SelectedValue == "0")
                    this.gvList.AddFilterExpression("Member.Account.Contains(@0)", this.tbAccount.Text);
                else
                    this.gvList.AddFilterExpression("Member.Account=@0", this.tbAccount.Text);
                this.MemberId = 0;
            }
            if (this.ddlTitle.SelectedValue != "")
                this.gvList.AddFilterExpression("Log.Title=@0", this.ddlTitle.Text);
            if (this.tbType.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Log.Title=@0", this.tbType.Text);
            if (this.tbBeginTime.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Log.AddTime>=@0", this.tbBeginTime.Text.Convert<DateTime>());
            if (this.tbEndTime.Text.EmptyString() != null)
                this.gvList.AddFilterExpression("Log.AddTime<=@0", this.tbEndTime.Text.Convert<DateTime>());
            TJ();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            MemberId = 0;
            ListBind();
        }


    }
}
